<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>健康医疗</title>
    <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no"/>
    <link rel="shortcut icon" href="../img/favicon.ico"/>
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../element-ui/index.css">
    <link rel="stylesheet" href="../css/style.css">
    <link rel="stylesheet" href="../css/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/page-health-order.css"/>
</head>
<body class="hold-transition">
<div id="app">
    <div class="content-header">
        <h1>预约管理<small>预约列表</small></h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item>首页</el-breadcrumb-item>
            <el-breadcrumb-item>预约管理</el-breadcrumb-item>
            <el-breadcrumb-item>预约列表</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="app-container">
        <div class="box">
            <div class="filter-container">
                <el-input v-model="queryString" placeholder="姓名/会员号" style="width: 200px;"
                          class="filter-item" @keyup.enter.native="handleCurrentChange(1)" clearable>
                </el-input>
                <el-button @click="handleCurrentChange(1)" class="dalfBut">查询</el-button>
                <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
            </div>
            <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                <el-table-column type="index" align="center" label="序号"></el-table-column>
                <el-table-column prop="member.name" label="姓名" align="center"></el-table-column>
                <el-table-column prop="memberId" label="会员号" align="center"></el-table-column>
                <el-table-column label="性别" align="center">
                    <template slot-scope="scope">
                        <span>{{ scope.row.member.sex == '0' ? '不限' : scope.row.member.sex == '1' ? '男' : '女'}}</span>
                    </template>
                </el-table-column>

                <el-table-column prop="setmealName" label="套餐名称" align="center"></el-table-column>
                <el-table-column prop="orderDate" label="预约时间" align="center"></el-table-column>
                <el-table-column prop="orderType" label="预约类型" align="center"></el-table-column>
                <el-table-column prop="orderStatus" label="到诊状态" align="center"></el-table-column>
                <el-table-column prop="member.phoneNumber" label="电话号码" align="center"></el-table-column>
                <el-table-column label="操作" width="160" align="left">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="handleUpdate(scope.row.id)">编辑</el-button>
                        <el-button size="mini" type="danger" @click="handleDelete(scope.row.id)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination-container">
                <el-pagination class="pagiantion"
                               :current-page="pageNow"
                               :page-size="pageSize"
                               :total="total"
                               layout="total, prev, pager, next, jumper"
                               @current-change="handleCurrentChange">
                </el-pagination>
            </div>
            <!-- 新增标签弹出层 -->
            <div class="add-form">
                <el-dialog title="新增预约" :visible.sync="dialogFormVisible">
                    <template>
                        <el-tabs v-model="activeName" type="card">
                            <!--第1个标签-->
                            <el-tab-pane label="基本信息" name="first">
                                <el-form ref="dataAddForm" :model="member" :rules="rules"
                                         label-position="right" label-width="100px">
                                    <!--套餐-->
                                    <div class="checkScrol">
                                        <div class="filter-container">
                                            <el-input v-model="queryStringSetMeal" placeholder="套餐名称/套餐详情"
                                                      style="width: 200px;"
                                                      class="filter-item" @keyup.enter.native="handleSetMeal"
                                                      clearable>
                                            </el-input>
                                            <el-button @click="handleSetMeal" class="dalfBut">查询套餐</el-button>
                                        </div>
                                        <el-table ref="singleTable" size="small" current-row-key="id"
                                                  :data="setmeals"
                                                  highlight-current-row
                                                  height="200px" row-key="id">
                                            <el-table-column prop="id" label="套餐id" align="center" width="75px">
                                                <template slot-scope="scope">
                                                    <el-button type="primary" size="mini" @click="findSetmealId(scope.row.id)">选中</el-button>
                                                </template>
                                            </el-table-column>
                                            <el-table-column prop="id" label="套餐id"
                                                             align="center" width="75px"></el-table-column>
                                            <el-table-column prop="name" label="套餐名称"
                                                             align="center" width="100px"></el-table-column>
                                            <el-table-column prop="remark" label="套餐具体内容"
                                                             align="center"></el-table-column>
                                        </el-table>
                                    </div>
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="电话号码" prop="phonenumber">
                                                <el-input v-model="member.phonenumber"/>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="姓名" prop="name">
                                                <el-input v-model="member.name"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>

                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="性别">
                                                <el-select v-model="member.sex">
                                                    <el-option label="男" value="1"></el-option>
                                                    <el-option label="女" value="2"></el-option>
                                                </el-select>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="预约时间">
                                                <div class="block">
                                                    <el-date-picker
                                                            v-model="order.orderDate"
                                                            type="date"
                                                            value-format="yyyy-MM-dd"
                                                            placeholder="选择日期">
                                                    </el-date-picker>
                                                </div>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                </el-form>
                            </el-tab-pane>
                        </el-tabs>
                    </template>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible = false">取消</el-button>
                        <el-button type="primary" @click="handleAdd()">确定</el-button>
                    </div>
                </el-dialog>
            </div>
            <!-- 编辑标签弹出层 -->
            <div class="add-form">
                <el-dialog title="编辑页面" :visible.sync="dialogFormVisible4Edit">
                    <template>
                        <el-tabs v-model="activeName" type="card">
                            <!--第1个标签-->
                            <el-tab-pane label="基本信息" name="first">
                                <el-form ref="dataEditForm" :model="order" :rules="rules"
                                         label-position="right" label-width="100px">
                                    <!--套餐-->
                                    <div class="checkScrol">
                                        <div class="filter-container">
                                            <el-input v-model="queryStringSetMeal" placeholder="套餐名称/套餐详情"
                                                      style="width: 200px;"
                                                      class="filter-item" @keyup.enter.native="handleSetMeal"
                                                      clearable>
                                            </el-input>
                                            <el-button @click="handleSetMeal" class="dalfBut">查询套餐</el-button>
                                        </div>
                                        <el-table ref="singleTable" size="small" current-row-key="id"
                                                  :data="setmeals"
                                                  highlight-current-row
                                                  height="200px" row-key="id">
                                            <el-table-column prop="id" label="套餐id" align="center" width="75px">
                                                <template slot-scope="scope">
                                                    <el-button type="primary" size="mini" @click="findSetmealId(scope.row.id)">选中</el-button>
                                                </template>
                                            </el-table-column>
                                            <el-table-column prop="id" label="套餐id"
                                                             align="center" width="75px"></el-table-column>
                                            <el-table-column prop="name" label="套餐名称"
                                                             align="center" width="100px"></el-table-column>
                                            <el-table-column prop="remark" label="套餐具体内容"
                                                             align="center"></el-table-column>
                                        </el-table>
                                    </div>
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="电话号码" prop="phonenumber" >
                                                <el-input v-model="member.phonenumber" disabled />
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="姓名" prop="name" >
                                                <el-input v-model="member.name" disabled />
                                            </el-form-item>
                                        </el-col>
                                    </el-row>

                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="性别" >
                                                <el-select v-model="member.sex" disabled >
                                                    <el-option label="男" value="1"></el-option>
                                                    <el-option label="女" value="2"></el-option>
                                                </el-select>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="预约时间">
                                                <div class="block">
                                                    <el-date-picker
                                                            v-model="order.orderDate"
                                                            type="date"
                                                            value-format="yyyy-MM-dd"
                                                            placeholder="选择日期"
                                                            :picker-options="pickerOptions">
                                                    </el-date-picker>
                                                </div>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                </el-form>
                            </el-tab-pane>
                        </el-tabs>
                    </template>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                        <el-button type="primary" @click="handleEdit()">确定</el-button>
                    </div>
                </el-dialog>
            </div>
        </div>
    </div>
</div>
</body>
<!-- 引入组件库 -->
<script src="../js/vue.js"></script>
<script src="../js/common.js"></script>
<script src="../js/axios.min.js"></script>
<script src="../element-ui/index.js"></script>
<script type="text/javascript" src="../js/jquery-3.5.1.js"></script>
<script>
    var vue = new Vue({
        el: '#app',
        data: {
            dataList: [], //当前页要展示的列表数据
            pageNow: 1,//当前页码
            pageSize: 8,//每页显示的记录数
            total: 0,//总记录数
            setmeals: [],//套餐详情
            queryStringSetMeal: "",//查询套餐条件
            queryString: "",//查询条件s
            queryOrders: "",//查询预约详情
            dataList: [],//当前页要展示的分页列表数据
            order: {},//预约表单数据
            member: {},//会员数据
            activeName: 'first',//添加/编辑窗口Tab标签名称
            currentRow: null,//当前行
            //orderDate: "",//预约时间
            currentMemberId: null,//当前套餐id
            orderDate_check: false, //体检日期校验
            order_check: false, //预约重复校验

            dialogFormVisible: false,//增加表单是否可见
            dialogFormVisible4Edit: false,//编辑表单是否可见
            rules: {//校验规则
                name: [{required: true, message: '姓名为必填项', trigger: 'blur'}],
                phonenumber: [{required: true, message: '电话号码为必填项', trigger: 'blur'}]
            }
        },
        //钩子函数，VUE对象初始化完成后自动执行
        created() {
            this.findPage();
            //加载套餐基本信息
            this.handleSetMeal();
        },
        methods: {
            //编辑的提交
            handleEdit() {
                this.$refs["dataEditForm"].validate((valid) => {
                    //表单校验通过
                    if (valid) {
                        //打开全屏遮罩
                        var fullscreenLoading = this.$loading();

                        //提交
                        var url = "../order/update";
                        axios.post(url, this.order)
                            .then(res => {
                                if (res.data.flag) {
                                    this.$message.success(res.data.msg);
                                    //关闭编辑窗口
                                    this.dialogFormVisible4Edit = false;
                                    //刷新列表
                                    this.findPage();
                                } else {
                                    this.$message.warning(res.data.msg);
                                }

                                //关闭全屏遮罩
                                fullscreenLoading.close();
                            })
                            .catch(error => {
                                this.$message.error("请求出错：" + error);
                                //关闭全屏遮罩
                                fullscreenLoading.close();
                            });
                    }
                });
            },
            //加载套餐
            handleSetMeal() {
                //打开全屏遮罩
                var fullscreenLoading = this.$loading();

                var url = "../setMeal/findByQuery?query=" + this.queryStringSetMeal;
                axios.get(url)
                    .then(res => {
                        if (res.data.flag) {
                            this.setmeals = res.data.data;
                        } else {
                            this.$message.warning(res.data.msg);
                        }
                        //关闭全屏遮罩
                        fullscreenLoading.close();
                    })
                    .catch(error => {
                        this.$message.error("请求出错：" + error);
                        //关闭全屏遮罩
                        fullscreenLoading.close();
                    });
            },

            //添加预约-预约表信息
            handleAdd() {
                this.$refs["dataAddForm"].validate(async valid => {
                    //表单校验通过
                    if (valid) {
                        //打开全屏遮罩
                        var fullscreenLoading = this.$loading();

                        //校验手机号输入是否正确
                        if (!checkTelephone(this.member.phonenumber)) {
                            this.$message.warning("请输入正确的手机号");
                            fullscreenLoading.close();
                            return false;
                        }
                        //校验体检日期
                        await this.checkOrderDate();
                        if (!this.orderDate_check) {
                            fullscreenLoading.close();
                            return false;
                        }
                        //获取会员id
                        await this.findMemberId();

                        //判断是否重复预约
                        await this.checkOrder();
                        if (!this.order_check) {
                            fullscreenLoading.close();
                            return false;
                        }

                        //提交
                        var url = "../order/add";
                        axios.post(url, this.order)
                            .then(res => {
                                if (res.data.flag) {
                                    this.$message.success(res.data.msg);
                                    //关闭新增窗口
                                    this.dialogFormVisible = false;
                                    //刷新列表
                                    this.findPage();
                                } else {
                                    this.$message.warning(res.data.msg);
                                }
                                //关闭全屏遮罩
                                fullscreenLoading.close();
                            })
                            .catch(error => {
                                this.$message.error("请求出错：" + error);
                                //关闭全屏遮罩
                                fullscreenLoading.close();
                            });
                    }
                });
            },

            data() {
                return {
                    pickerOptions:{                                 //禁用当前日期之前的日期
                        disabledDate(time) {
                            //Date.now()是javascript中的内置函数，它返回自1970年1月1日00:00:00 UTC以来经过的毫秒数。
                            return time.getTime() < Date.now() - 8.64e7;
                        },
                    },
                }
            },
            //判断是否重复预约
            async checkOrder() {
                await axios.post("../order/checkOrder", this.order)
                    .then(res => {
                        if (res.data.flag) {
                            this.order_check = true;
                        } else {
                            this.order_check = false;
                            this.$message.warning(res.data.msg);
                        }
                    })
                    .catch(error => {
                        this.order_check = false;
                        this.$message.error("请求出错：" + error);
                    });
            },
            //获取会员id
            async findMemberId() {
                await axios.post("../member/findMemberId", this.member)
                    .then(res => {
                        if (res.data.flag) {
                            this.order.memberId = res.data.data;
                        } else {
                            this.$message.warning(res.data.msg);
                        }
                    })
                    .catch(error => {
                        this.$message.error("请求出错：" + error);
                    });
            },
            async findSetmealId(id){
                this.order.setmealId = id;
            },
            //校验体检日期
            async checkOrderDate() {
                var orderDate = this.order.orderDate;
                if (orderDate == null || orderDate == "") {
                    this.$message.warning("请选择体检日期");
                    this.orderDate_check = false;
                    return false;
                }

                var url = "../orderSetting/checkOrderDate?orderDate=" + orderDate;
                await axios.get(url)
                    .then(res => {
                        if (res.data.flag) {
                            this.orderDate_check = true;
                        } else {
                            this.orderDate_check = false;
                            this.$message.warning(res.data.msg);
                        }
                    })
                    .catch(error => {
                        this.orderDate_check = false;
                        this.$message.error("请求出错：" + error);
                    });
            },

            //分页查询
            findPage() {
                //打开全屏遮罩
                var fullscreenLoading = this.$loading();

                var url = "../order/findPage/" + this.pageNow + "/" + this.pageSize
                    + "?" + "query=" + this.queryString;
                axios.get(url)
                    .then(res => {
                        if (res.data.flag) {
                            // 分页的结果
                            this.dataList = res.data.data.rows;
                            // 总记录
                            this.total = res.data.data.total;
                        } else {
                            this.$message.warning(res.data.msg);
                        }
                        //关闭全屏遮罩
                        fullscreenLoading.close();
                    })
                    .catch(error => {
                        this.$message.error("请求出错：" + error);
                        //关闭全屏遮罩
                        fullscreenLoading.close();
                    });
            },
            // 重置表单
            resetForm() {
                // 清空表单内容
                this.order = {};
                this.member = {};
                //默认选中套餐信息标签页
                this.activeName = "first";
            },
            // 弹出编辑窗口
            handleUpdate(id) {
                //重置表单，弹出编辑窗口
                this.resetForm();
                this.dialogFormVisible4Edit = true;
                //加载要编辑的预约
                this.handleOrder(id);
                //加载预约的套餐
                this.handleSetMeal(id);
            },
            // 弹出添加窗口
            handleCreate() {
                // 弹出新增窗口
                this.dialogFormVisible = true;
                // 重置表单
                this.resetForm();
            },
            //切换页码
            handleCurrentChange(currentPage) {
                this.pageNow = currentPage;
                this.findPage();
            },

            //加载被编辑的预约
            handleOrder(id) {
                //打开全屏遮罩
                var fullscreenLoading = this.$loading();
                //通过id查询预约
                axios.get('../order/findById/' + id)
                    .then(res => {
                        if (res.data.flag) {
                            //套餐
                            this.order = res.data.data;
                        } else {
                            this.$message.warning(res.data.msg);
                        }
                        //关闭全屏遮罩
                        fullscreenLoading.close();
                    })
                    .catch(error => {
                        this.$message.error("请求出错：" + error);
                        //关闭全屏遮罩
                        fullscreenLoading.close();
                    });
                this.findMemberByOrderId(this.order.memberId);

            },
            findMemberByOrderId(id){

                axios.get('../member/findById/' + id)
                    .then(res => {
                        if (res.data.flag) {
                            //套餐
                            this.member = res.data.data;
                        } else {
                            this.$message.warning(res.data.msg);
                        }
                    })
                    .catch(error => {
                        this.$message.error("请求出错：" + error);
                        //关闭全屏遮罩
                        fullscreenLoading.close();
                    });
            },
            // 删除
            handleDelete(id) {
                this.$confirm('此操作将【永久删除】该预约123123, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    //打开全屏遮罩
                    var fullscreenLoading = this.$loading();

                    // 提交删除
                    axios.delete("../order/deleteById/" + id)
                        .then(res => {
                            if (res.data.flag) {
                                this.$message.success(res.data.msg);
                                //刷新列表
                                this.findPage();
                            } else {
                                this.$message.warning(res.data.msg);
                            }

                            //关闭全屏遮罩
                            fullscreenLoading.close();
                        })
                        .catch(error => {
                            this.$message.error("请求出错：" + error);
                            //关闭全屏遮罩
                            fullscreenLoading.close();
                        });
                }).catch(() => {
                });
            }
        }
    })
</script>
</html>